﻿using System;
using System.Drawing;
using Microsoft.Xna.Framework;
using System.Windows.Forms;

namespace LevelEdtor
{
    /// <summary>
    /// Defines a control that allows the user to visualy select an a range of values from 0 to 360
    /// </summary>
    public class AnglePicker : Control
    {
        private float _minAngle = -5, _maxAngle = 5;
        /// <summary>
        /// the lower-bound range, in degrees, selected by the user
        /// </summary>
        public float MinAngle
        {
            get { return _minAngle; }

            set 
            {
                AngleChanged(this, new EventArgs());
                _minAngle = value; 
            }
        }

        /// <summary>
        /// the upper-bound range, in degrees, selected by the user
        /// </summary>
        public float MaxAngle
        {
            get { return _maxAngle; }

            set 
            {
                AngleChanged(this, new EventArgs());
                _maxAngle = value; 
            }
        }

        private float radius
        {
            get { return Width / 2.0f - 4.0f; }
        }

        private PointF center
        {
            get { return new PointF(ClientRectangle.Width / 2.0f, ClientRectangle.Height / 2.0f); }
        }

        private PointF point1
        {
            get { return new PointF(center.X + (float)Math.Cos(MathHelper.ToRadians(_minAngle)) * radius, center.Y + (float)Math.Sin(MathHelper.ToRadians(_minAngle)) * radius); }
        }

        private PointF point2
        {
            get { return new PointF(center.X + (float)Math.Cos(MathHelper.ToRadians(_maxAngle)) * radius, center.Y + (float)Math.Sin(MathHelper.ToRadians(_maxAngle)) * radius); }
        }

        private bool pickUpMin, pickupMax;

        public event EventHandler AngleChanged;

        public AnglePicker()
        {
            AngleChanged += AnglePicker_AngleChanged;
            Paint += AnglePicker_Paint;
            MouseMove += AnglePicker_MouseMove;
        }

        void AnglePicker_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Vector2 v = new Vector2(e.Location.X, e.Location.Y) - new Vector2(ClientRectangle.Width / 2.0f, ClientRectangle.Height / 2.0f);
                float angle = MathHelper.ToDegrees((float)Math.Atan2(v.Y, v.X));
                if (!pickUpMin && !pickupMax)
                {

                    if (MathHelper.Distance(angle, MaxAngle) < 10)
                        pickupMax = true;
                    if (MathHelper.Distance(angle, MinAngle) < 10)
                        pickUpMin = true;
                }
                else if (pickUpMin)
                    MinAngle = angle;
                else if (pickupMax)
                    MaxAngle = angle;
            }
            else
                pickupMax = pickUpMin = false;
        }

        void AnglePicker_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            Pen p = new Pen(Brushes.Black);
            p.Width = 5.0f;
            g.FillEllipse(Brushes.DarkGray, new RectangleF(ClientRectangle.X + 2, ClientRectangle.Y + 2, ClientRectangle.Width - 4, ClientRectangle.Height - 4));
            g.FillPie(Brushes.Red, ClientRectangle, _minAngle, _maxAngle - _minAngle);

            g.DrawLine(Pens.Black, new PointF(Width / 2.0f, Height / 2.0f), point1);
            g.DrawLine(Pens.Black, new PointF(Width / 2.0f, Height / 2.0f), point2);
            g.DrawEllipse(p, new RectangleF(ClientRectangle.X + 2, ClientRectangle.Y + 2, ClientRectangle.Width - 4, ClientRectangle.Height - 4));
        }

        void AnglePicker_AngleChanged(object sender, EventArgs e)
        {
            Invalidate();
        }
    }
}